﻿using System.Collections.Generic;
using System.Threading.Tasks;
using Batman.BLL.UserModule.Extensions;
using Batman.DLL.Todo;
using Batman.SQLScripts;
using Dapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Batman.Web.Api.Todo.Controllers
{
    [ApiController]
    [Route("todo/[controller]")]
    public class TodoController : ControllerBase
    {
        private readonly TodoDbContext _todoDbContext;

        public TodoController(TodoDbContext todoDbContext)
        {
            this._todoDbContext = todoDbContext;
        }

        [HttpGet]
        public async Task<List<object>> Select()
        {
            var parameters = new Dictionary<string, object>();

            parameters.Add("userId", Request.HttpContext.User.GetUserId());

            foreach (var item in Request.Query)
            {
                parameters.Add(item.Key, item.Value);
            }
            var commandTexts = CommonDataSelect.GetCommands(parameters);

            var results = new List<object>();
            using (_todoDbContext)
            {
                foreach (var commandText in commandTexts)
                {
                    var res = await _todoDbContext.Database.GetDbConnection().QueryAsync(commandText, parameters);
                    results.Add(res);
                }

                return results;
            }
        }
    }
}
